package com.ruoyi.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.service.impl.SysDictDataServiceImpl;
import com.ruoyi.utils.SimpleResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.AdvProducts;
import com.ruoyi.service.IAdvProductsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 *  Controller
 *
 * @author ruoyi
 * @date 2024-05-13
 */
@RestController
@RequestMapping("/system/products")
public class AdvProductsController extends BaseController
{
    @Autowired
    private IAdvProductsService advProductsService;

    @Autowired
    private SysDictDataServiceImpl sysDictDataService;

    /**
     * 查询 列表
     */
    @PreAuthorize("@ss.hasPermi('system:products:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdvProducts advProducts)
    {
        startPage();
        List<AdvProducts> list = advProductsService.selectAdvProductsList(advProducts);
        return getDataTable(list);
    }

    /**
     * 查询 列表
     */
    @PreAuthorize("@ss.hasPermi('system:products:list')")
    @GetMapping("/list/app")
    public TableDataInfo listApp(AdvProducts advProducts)
    {
        startPage();
        List<AdvProducts> list = advProductsService.selectAdvProductsListApp(advProducts);
        return getDataTable(list);
    }


    /**
     * 导出 列表
     */
    @PreAuthorize("@ss.hasPermi('system:products:export')")
    @Log(title = " ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdvProducts advProducts)
    {
        List<AdvProducts> list = advProductsService.selectAdvProductsList(advProducts);
        ExcelUtil<AdvProducts> util = new ExcelUtil<AdvProducts>(AdvProducts.class);
        util.exportExcel(response, list, " 数据");
    }

    /**
     * 获取 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:products:query')")
    @GetMapping(value = "/{advProductsId}")
    public AjaxResult getInfo(@PathVariable("advProductsId") Long advProductsId)
    {
        return success(advProductsService.selectAdvProductsByAdvProductsId(advProductsId));
    }

    /**
     * 新增
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('system:products:add')")
    @Log(title = " ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdvProducts advProducts)
    {
        System.out.println("新增的产品---------->"+advProducts);
        return toAjax(advProductsService.insertAdvProducts(advProducts));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('system:products:edit')")
    @Log(title = " ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdvProducts advProducts)
    {
        return toAjax(advProductsService.updateAdvProducts(advProducts));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('system:products:remove')")
    @Log(title = " ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{advProductsIds}")
    public AjaxResult remove(@PathVariable Long[] advProductsIds)
    {
        return toAjax(advProductsService.deleteAdvProductsByAdvProductsIds(advProductsIds));
    }

    /**
     * 查询 列表 收藏
     */
    @PreAuthorize("@ss.hasPermi('system:products:list')")
    @GetMapping("/findcollect/{advMembersId}/{productType}")
    public TableDataInfo findcollect(@PathVariable("advMembersId") Long advMembersId,
                                     @PathVariable("productType")  Long productType)
    {
        startPage();
        List<AdvProducts> list = advProductsService.findcollect(advMembersId, productType);
        return getDataTable(list);
    }

    /**
     * 根据设备产品编号查询设备中所包含的内容产品
     * @param devicesno
     * @return
     */
    @GetMapping("/findbuy/withdevicesno/{devicesno}/{advMembersId}")
    public TableDataInfo findBuyWithDevicesno(@PathVariable("devicesno") String devicesno,
                                              @PathVariable("advMembersId") String advMembersId) {
        List<AdvProducts> list = advProductsService.findBuyWithDevicesno(devicesno, advMembersId);
        return getDataTable(list);
    }

    /**
     * 判断是否购买某个内容产品
     * @return
     */
    @GetMapping("/isbuy/{advMembersId}/{advProductsId}")
    public SimpleResult isBuy(@PathVariable("advMembersId") Long advMembersId,
                              @PathVariable("advProductsId") Long advProductsId) {
        AdvProducts products = advProductsService.isBuy(advMembersId, advProductsId);
        if(products == null) {
            return SimpleResult.success(0, "没有购买");
        } else {
            return SimpleResult.success(1, "购买过了");
        }
    }

    @GetMapping("/findcolor")
    public SimpleResult findColor() {
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("sys_product_color");
        List<SysDictData> result = sysDictDataService.selectDictDataList(sysDictData);
        return SimpleResult.success(result);
    }

    @GetMapping("/findmaterail")
    public SimpleResult findMaterial() {
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("sys_product_material");
        List<SysDictData> result = sysDictDataService.selectDictDataList(sysDictData);
        return SimpleResult.success(result);
    }

    @GetMapping("/getorderproduct")
    public SimpleResult getOrderProduct() {
        List<AdvProducts> advProducts = advProductsService.getOrderProduct();
        return SimpleResult.success(advProducts);
    }
}





